package com.anuyun.ms.base.aspect;



import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.anuyun.ms.base.common.BusinessException;
import com.anuyun.ms.base.common.Result;
import com.anuyun.ms.base.constant.ResultConstant;

@Aspect
@Component
public class RestExceptionHandlerAspect {
	

	/**
	 * 注解切点
	 */
	@Pointcut("@within(com.anuyun.ms.base.annotation.RestExceptionHandler) "
			+ "|| @annotation(com.anuyun.ms.base.annotation.RestExceptionHandler)")
	public void point() {
	}

	@Around("point()")
	public Object serviceAOP(ProceedingJoinPoint joinPoint) {
		try {
			return joinPoint.proceed();
		} catch (Throwable e) {
			if (e instanceof BusinessException) {
				return new Result<>(((BusinessException) e).getCode(), e.getMessage());
			} else {
				return new Result<>(ResultConstant.E, "系统错误: " + e.getMessage());
			}
		}

	}

}
